﻿#region Software License
/**************************************************************/
/*          Business of Software Consulting                   */
/*          FREE SOFTWARE LICENSE                             */
/**************************************************************/
/*  Directory Manifest Generator                              */
/*  by: Mike Cating                                           */
/*  © 2009 Business of Software Consulting                    */
/*                                                            */
/* BUSINESS OF SOFTWARE CONSULTING DISCLAIMS ALL WARRANTIES,  */
/* RESPONSIBILITIES, AND LIABILITIES ASSOCIATED WITH          */
/* USE OF THIS CODE IN ANY WAY, SHAPE, OR FORM                */
/* REGARDLESS HOW IMPLICIT, EXPLICIT, OR OBSCURE IT           */
/* IS. IF THERE IS ANYTHING QUESTIONABLE WITH REGARDS         */
/* TO THIS SOFTWARE BREAKING AND YOU GAIN A LOSS OF           */
/* ANY NATURE, WE ARE NOT THE RESPONSIBLE PARTY. USE          */
/* OF THIS SOFTWARE CREATES ACCEPTANCE OF THESE TERMS         */
/*                                                            */
/* USE OF THIS CODE MUST RETAIN ALL COPYRIGHT NOTICES         */
/* AND LICENSES (MEANING THIS TEXT).                          */
/*                                                            */
/**************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Permissions;

namespace BOSTools.DirectoryManifestGenerator
{
    public class DirectoryManifestController
    {
        FileDetailReader _reader;
        XmlManifestWriter _writer;

        public DirectoryManifestController(string sourceDir, string outputFile)
        {
            if ( string.IsNullOrEmpty( sourceDir ) )
            {
                throw new ArgumentException( "sourceFile was null or empty" );
            }

            if ( string.IsNullOrEmpty( outputFile ) )
            {
                throw new ArgumentException( "outputFile was null or empty" );
            }

            sourceDir = Path.GetFullPath(sourceDir);

            _reader = new FileDetailReader(sourceDir);
            _reader.DirectoryProcessed += new EventHandler<DirectoryEventArgs>( DirectoryProcessed );
            _writer = new XmlManifestWriter(sourceDir, outputFile);
        }

        [FileIOPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public void Process()
        {
            FileDetail detail = _reader.GetNextFile();
            while (detail != null)
            {
                _writer.AddFileDetail(detail);
                detail = _reader.GetNextFile();
            }

            _writer.Close();
        }

        private void DirectoryProcessed(object sender, DirectoryEventArgs e)
        {
            _writer.AddDirectory(e.DirectoryName);
        }
    }
}
